package com.augurit.common.sysmeta.convert;

import java.util.ArrayList;
import java.util.List;

import com.augurit.common.base.extmodel.inner.ExtMetaOwnerNode;
import com.augurit.common.base.extmodel.inner.ExtMetaTableNode;
import com.augurit.common.entity.SysMetaTable;
import com.augurit.common.sysmeta.web.form.SysMetaTableForm;
import com.augurit.common.util.lang.StringUtils;

public class SysMetaTableConvertor {
	
	/**
	 * 将entity转换为form
	 * @param entity
	 * @return
	 */
	public static SysMetaTableForm convertToForm(SysMetaTable entity){
		SysMetaTableForm form = null;
		if(entity != null){
			form = new SysMetaTableForm();
			form.setId(entity.getId());
			form.setTableName(entity.getTableName());
			form.setTableChineseComments(entity.getTableChineseComments());
			form.setOwner(entity.getOwner());
			form.setMemo(entity.getMemo());
			form.setEntityName(entity.getEntityName());
		}
		return form;
	}
	
	/**
	 * 
	 * @param form
	 * @param entity
	 */
	public static void  convertToEntity(SysMetaTableForm form,SysMetaTable entity){
		if(form != null && entity != null){
			if(form.getId() != null)
				entity.setId(form.getId());
			if(form.getTableName() != null && form.getTableName().trim().length()>0)
				entity.setTableName(form.getTableName().toUpperCase());
			if(form.getTableChineseComments()!= null && form.getTableChineseComments().trim().length()>0)
				entity.setTableChineseComments(form.getTableChineseComments());
			if(form.getOwner() != null && form.getOwner().trim().length()>0)
				entity.setOwner(form.getOwner());
			if(form.getMemo() != null && form.getMemo().trim().length()>0)
				entity.setMemo(form.getMemo());
			if(StringUtils.isNotBlank(form.getEntityName())){
				entity.setEntityName(form.getEntityName());
			}
		}
	}
	
	/**
	 * 将entity对象列表转换成form对象列表
	 * @param entities
	 * @return
	 */
	public static List<SysMetaTableForm> convertEntityListToFormList(List<SysMetaTable> entities){
		List<SysMetaTableForm> forms = null;
		if(entities != null && entities.size()>0){
			forms = new ArrayList<SysMetaTableForm>();
			for(SysMetaTable entity : entities){
				SysMetaTableForm form = convertToForm(entity);
				forms.add(form);
			}
		}
		return forms;
	}

	/**
	 *  将form对象转换成ext树节点对象
	 * @param form
	 * @return
	 */
	public static ExtMetaTableNode convertToExtNode(SysMetaTableForm form){
		ExtMetaTableNode node = null;
		if(form != null){
			node = new ExtMetaTableNode();
			node.setId(ExtMetaTableNode.TAG + form.getId());
			node.setIconCls(ExtMetaTableNode.ICON);
			if(form.getTableChineseComments() != null && form.getTableChineseComments().trim().length()>0)
				node.setText(form.getTableName() + "(" +form.getTableChineseComments().trim() + ")" );
			else
				node.setText(form.getTableName());
			node.setQtip(form.getTableChineseComments());
		}
		return node;
	}
	
	/**
	 * 将form列表转换成ext树节点列表
	 * @param forms
	 * @return
	 */
	public static List<ExtMetaTableNode> convertToExtNodeList(List<SysMetaTableForm> forms){
		List<ExtMetaTableNode> nodes = null;
		if(forms != null && forms.size()>0){
			nodes = new ArrayList<ExtMetaTableNode>();
			for(SysMetaTableForm form :forms){
				ExtMetaTableNode node = convertToExtNode(form);
				nodes.add(node);
			}
		}
		return nodes;
	}
	
	/**
	 *  将用户名列表转换为元数据树Ext用户节点列表
	 * @param list
	 * @return
	 */
	public static List<ExtMetaOwnerNode> convertToExtOwnerNodeList(List<String> list){
		List<ExtMetaOwnerNode> nodes = null;
		if(list != null && list.size()>0){
			nodes = new ArrayList<ExtMetaOwnerNode>();
			for(String owner:list){
				ExtMetaOwnerNode node = convertToExOwnertNode(owner);
				nodes.add(node);
			}
		}
		return nodes;
	}
	
	/**
	 * 将用户名转换为元数据树Ext用户节点
	 * @param owner
	 * @return
	 */
	public static ExtMetaOwnerNode convertToExOwnertNode(String owner){
		ExtMetaOwnerNode node = null;
		if(owner != null && owner.trim().length()>0){
			node =  new ExtMetaOwnerNode();
			node.setId(ExtMetaOwnerNode.TAG + owner);
			node.setText(owner);
			node.setLeaf(false);
		}
		return node;
	}
}
